package com.moyou.weichat.utils;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/1/19 0019.
 */

public class EditTextUtils {

    private static EditTextUtils editTextUtils;

    public static  EditTextUtils getEditTextUtils(){
        return editTextUtils == null ? editTextUtils =new EditTextUtils() : editTextUtils;
    }


    /**
     * 禁止EditText输入空格
     * @param editText
     */
    public static void setEditTextInhibitInputSpace(EditText editText){
        InputFilter filter=new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" "))
                return "";
                else
                return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

    /**
     * 禁止EditText输入特殊字符
     * @param editText
     */
    public static void setEditTextInhibitInputSpeChat(EditText editText){
        InputFilter filter=new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String speChat="[`\\[\\]/~￥（）——+|{}【】‘；：”“’#*]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(source.toString());
                if(matcher.find())return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

    /**
     * 这是限制回车
     * @param editText
     */
    public static void setEditTextInputEbter(EditText editText){
                //这里是限制回车！
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });
    }

    /**
     * 强制隐藏输入法
     * @param view 监听的那个view
     */
    public static void hideInput(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
        }
    }

    /**
     * 限制字数和空客
     * @param changeUserName
     * @param num
     * @param context
     */
    public static void setEditTextInputNuamber(final EditText changeUserName, final int num ,
                                               final Context context, final String type, final TextView textView){
        changeUserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged( CharSequence s , int start , int count, int after){

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = changeUserName.getText();
                if (type.equals("yes")) {
                    if (s.toString().contains(" ")) {
                        String[] str = s.toString().split(" ");
                        String str1 = "";
                        for (int i = 0; i < str.length; i++) {
                            str1 += str[i];
                        }
                        changeUserName.setText(str1);
                    }
                }

                int len = editable.length();
                if (textView != null) {
                    textView.setText(len + "/" + num);
                }
                if (len > num) {
                    T.Tips("输入最大为"+ (num ) +"个字哟！",context);
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, num);
                    changeUserName.setText(newStr);
                    editable = changeUserName.getText();
                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen){
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable , selEndIndex);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 弹出软键盘！
     * @param editText
     */
    public static void showInput(final EditText editText){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager m = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 300);


    }
    /**
     * EditText竖直方向是否可以滚动
     * @param editText  需要判断的EditText
     * @return  true：可以滚动   false：不可以滚动
     */
    public static boolean canVerticalScroll(EditText editText) {
        //滚动的距离
        int scrollY = editText.getScrollY();
        //控件内容的总高度
        int scrollRange = editText.getLayout().getHeight();
        //控件实际显示的高度
        int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom();
        //控件内容总高度与实际显示高度的差值
        int scrollDifference = scrollRange - scrollExtent;

        if(scrollDifference == 0) {
            return false;
        }

        return (scrollY > 0) || (scrollY < scrollDifference - 1);
    }
}
